import item from '../base/item'
import { UI_Listen,SKILLKARDBG } from '../../config/ui_def'
cc.Class({
    extends: item,

    properties: {
        light: cc.Node,
        enough_sprite: cc.Sprite,
        starBG_node: cc.Node,
        star_node: cc.Node,
        haveLight: cc.Node,
        //isteam
        isTeam: {
            default: false,
            visible: true
        },
    },

    // LIFE-CYCLE CALLBACKS:
    showLight() {
        this.light.active = true
        this.isChoose = true
    },

    haveLightShow() {
        this.haveLight.active = true
        this.haveLight.getChildByName('red').active = false
        this.haveLight.getChildByName('green').active = true
    },

    dontHaveLight() {
        this.haveLight.active = true
        this.haveLight.getChildByName('red').active = true
        this.haveLight.getChildByName('green').active = false
    },


    hideLight() {
        this.light.active = false
        this.isChoose = false
        this.isCanClick = true
    },

    cantClick() {
        this.isCanClick = false
    },


    showItem() {
        this.head_sp.node.active = true
    },

    hideItem() {
        this.light.active = false
        this.head_sp.node.active = false
        this.star_node.active = false
        this.starBG_node.active = false
        this.bg_sp.node.active = true
        this.info = null
    },

    cleserItem() {

    },

    infoInit(info, index , location) {
        // console.log(info, index,location)
        if (this.haveLight) {
            this.haveLight.active = false
        }
        if (location){
            this.cardLocation = SKILLKARDBG[location]
        }

        // console.log(this.cardLocation)

        if (info) {
            this.bg_sp.node.active = false
            this.info = info
            let cardinfo = he.db.getDataByArgs("Card", { cardId: info.id, level: info.lv })
            this.head_sp.node.active = true
            this.head_sp.spriteFrame = he.load.get('skill', cc.SpriteAtlas, 'plist').getSpriteFrame(info.id)
            this.star_node.active = true
            this.starBG_node.active = true
            this.isCanClick = true
            this.setstar()
        } else {
            this.info=null
            this.isTeam = false
            this.star_node.active = false
            this.starBG_node.active = false
            this.bg_sp.node.active = true
            this.head_sp.node.active = false
            this.isCanClick = false
            this.head_sp.spriteFrame = null
            this.setstar(0)
        }
        if (index >= 0) {
            this.index = index
        }
        if (info) {
            if (info.index) {
                this.isTeam = true
            } else {
                this.isTeam = false
            }
        }

        this.isChoose = false

    },
    showCheckBorder(param) {
        this.enough_sprite.node.active = true
        if (gg.data.checkItem(param)) {
            this.enough_sprite.spriteFrame = he.load.get('item', cc.SpriteAtlas, 'plist').getSpriteFrame("green")
        } else {
            this.enough_sprite.spriteFrame = he.load.get('item', cc.SpriteAtlas, 'plist').getSpriteFrame("red")
        }
    },

    setstar(num) {
        let starNum = 0
        if (num >= 0) {
            starNum = num
        } else {
            starNum = this.info.lv
        }
        for (let i = 0; i < 5; i++) {
            this.star_node.children[i].active = false
        }
        for (let j = 0; j < starNum; j++) {
            this.star_node.children[j].active = true
        }
    },

    clickServer() {
        // console.log(this.isTeam,this.index)
        // console.log(this.info)
        if (this.isChoose) {
            this.isChoose = false
        }
        if (this.info) {
            let typeinfo = this.isTeam ? UI_Listen.SKILL_TEAMCARD : UI_Listen.SKILL_BAGCARD
            gg.eventManager.emit('card', { info: this.info, index: this.index, type: typeinfo })
        }
        else{
            if(this.cardLocation){
                if(!this.isTeam){
                    let typeinfo = this.cardLocation
                    // console.log(this.index)
                    gg.eventManager.emit('card', { info: null, index: this.index,  type: typeinfo })
                }
            }
            else{
                if(!this.isTeam){
                    let typeinfo = SKILLKARDBG[2]
                    gg.eventManager.emit('card', { info: null, index: null,  type: typeinfo })
                }
            }
        }
    },
});
